package com.kingyea.mobilepolice.endpoint;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.provider.endpoint.FrameworkEndpoint;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by wangyunfei on 2017/7/24.
 */
@FrameworkEndpoint
@SessionAttributes("authorizationRequest")
public class RevokeTokenEndpoint {

    @Autowired
    @Qualifier("consumerTokenServices")
    ConsumerTokenServices consumerTokenServices;

    @RequestMapping(method = RequestMethod.DELETE, value = "/oauth/token")
    @ResponseBody
    public String revokeToken(String access_token) {
        if (consumerTokenServices.revokeToken(access_token)) {
            return "注销成功";
        } else {
            return "注销失败";
        }
    }


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("login !!! ... ");
        System.out.println(JSON.toJSONString(model));
        System.out.println(request.getAttribute("authorizationRequest"));
        System.out.println(request.getAttribute("_csrf"));
        System.out.println(request.getRequestURI());
        System.out.println(JSON.toJSONString(request.getAttributeNames()));
        System.out.println(JSON.toJSONString(request.getHeaderNames()));
        System.out.println(JSON.toJSONString(request.getServletPath()));
        System.out.println(JSON.toJSONString(request.getUserPrincipal()));
//        return new ModelAndView("login",new HashMap<String,Object>(){{put("hello","world");}});
        return "login";
    }


    @RequestMapping(value = "/oauth/confirm_access", method = RequestMethod.GET)
    public String confirm_access(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("confirm_access !!! ... ");
        System.out.println(JSON.toJSONString(model));
        System.out.println(request.getRequestURI());
        System.out.println(JSON.toJSONString(request.getAttributeNames()));
        System.out.println(JSON.toJSONString(request.getHeaderNames()));
        System.out.println(JSON.toJSONString(request.getServletPath()));
        System.out.println(JSON.toJSONString(request.getUserPrincipal()));
//        return new ModelAndView("authorize",new HashMap<String,Object>(){{put("hello","world");}});
        return "authorize";
    }


}
